package client;

import java.io.IOException;
import java.util.concurrent.LinkedBlockingQueue;
import communication.Message;
import communication.client.ServerHandle;

public class MessageSender implements Runnable {
	private LinkedBlockingQueue<Message> recievedMessages;
	//private LinkedBlockingQueue<Message> sendedMessages;
	private ServerHandle serverHandle;
	private PaperSoccerAI paperSoccerAI;

	public MessageSender(ServerHandle serverHandle,
			LinkedBlockingQueue<Message> recievedMessages,
			LinkedBlockingQueue<Message> sendedMessages,
			PaperSoccerAI paperSoccerAI) {
		this.recievedMessages = recievedMessages;
		//this.sendedMessages=sendedMessages;
		this.serverHandle = serverHandle;
		this.paperSoccerAI=paperSoccerAI;
	}
	
	@Override
	public void run() {
		while (true) {
			Message msg = recievedMessages.peek();
			if (msg.getType().toString().equalsIgnoreCase("gamestate")) {
				try {
					Message sendMessage=paperSoccerAI.GetAIResponse(msg);
					serverHandle.sendMove(sendMessage.getGameId(), sendMessage.getMove());
					
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			else
			{
				System.out.println(msg.toString());
			}
		}
	}
}
